* This do file creates figures and tables related to the seed demand auction
* Paper Table 4 and Figure 5

local bdm_controls i.gender i.caste i.education hh_size

use "$out_data/auction_price_data.dta", clear

* Declined to participate
gen byte decline = 0
egen byte decline_reason = rowmax(participate_reason_2 participate_reason_3 participate_reason_4 participate_reason_5 participate_reason_6 participate_reason_10 participate_reason_12 participate_reason__997)
replace decline = 1 if decline_reason==1

* Treatment assignments
rename treat_code treat_detail
recode treat_detail (0=5 "Control") (1 2 3=1 "Treated") (4=2 "Reserve"), gen(treat_input)
recode treat_detail (0=0 "Control") (1 2 3=1 "Treated") (4=.), gen(Treat)
gen vilid = 10000*district + 1000*block + village
rename price Price

* Auction + saved seeds
gen quantZS = quantZ + saved_seed

* Add controls for unsurveyed
rename (age education gender) (m_age m_education m_gender)
merge m:1 hhid using "$out_data/survey_roster.dta", keepusing(m_age m_education m_gender caste hh_size) update nogen
rename (m_age m_education m_gender) (age education gender)

recode education (-997=1)

*****
* Run Regressions
*****

eststo clear
* Attrition by experiment farmers
preserve
keep if farmer_old==1
collapse participate decline Treat , by(*_code vilid hhid crop)

eststo: areg participate Treat, a(block_code) vce(cluster vilid)
quietly sum participate if Treat==0
estadd scalar ctrl_mean = `r(mean)'

restore

* Analysis including snowball sample
foreach vbl in quantZ quantZS {
	eststo: areg `vbl' Treat ib140.Price i.crop `bdm_controls', a(block_code) vce(cluster vilid)
	quietly sum `vbl' if Treat==0 & Price==140
	estadd scalar ctrl_mean = `r(mean)'
}

local estset replace se tex label  mlabels(none) nonotes nonumbers starlevels(* 0.1 ** 0.05 *** 0.01)
local mainstats stats(ctrl_mean r2 N, fmt(2 2 0) labels("Control Mean" "R-Squared" "Observations")) 
local ecell ""

local mgset prefix(\multicolumn{@span}{c}{) suffix(}) span
local col1 "  & Participate & Purchased & Total \\"
local col2 " & (1) & (2) & (3) \\ \midrule "
local rules \cmidrule(lr){3-4}
local est_adopt mgroups("Survey" "Seed Quantity (Kg.)", pattern(1 1 0) `mgset' end( `rules') ) posthead(`col1' `col2')

label var Treat "Treat"

esttab using "$out_tables/Table_4.tex", keep(Treat *.Price) `estset' `mainstats' `ecell' `est_adopt'

*****
* Make Inverse Demand Curves
*****
collapse (mean) quantZ quantZS (semean) se_Z=quantZ se_S=quantZS, by(crop Price Treat)
local grset graphregion(color(white)) ylabel(0(0.5)3, angle(0)) plotregion(margin(0))
graph drop _all

sort crop Price
twoway (connected quantZ Price if crop==1 & Treat==1, color(gs0) lpattern(dash) msymbol(t)) ///
	(connected quantZ Price if crop==1 & Treat==0, color(gs0) lwidth(thin) msymbol(o)) , ///
	`grset' ytitle("") xtitle("Price")  name(pDem)  /// 
	legend(order(2 "Control" 1 "Treated")) title("Pigeon Pea (kgs.)")
graph export "$out_figures/Parts/fig_bdm_pp.png", replace

twoway (connected quantZ Price if crop==2 & Treat==1, color(gs0) lpattern(dash) msymbol(t)) ///
	(connected quantZ Price if crop==2 & Treat==0, color(gs0) lwidth(thin) msymbol(o)) , ///
	`grset' ytitle("") xtitle("Price")  name(bDem)  /// 
	legend(order(2 "Control" 1 "Treated")) title("Black Gram (kgs.)")
graph export "$out_figures/Parts/fig_bdm_bg.png", replace

twoway (connected quantZ Price if crop==3 & Treat==1, color(gs0) lpattern(dash) msymbol(t)) ///
	(connected quantZ Price if crop==3 & Treat==0, color(gs0) lwidth(thin) msymbol(o)) , ///
	`grset' ytitle("") xtitle("Price")  name(lDem)  /// 
	legend(order(2 "Control" 1 "Treated")) title("Lentil (kgs.)")
graph export "$out_figures/Parts/fig_bdm_ll.png", replace
 
	
grc1leg pDem bDem lDem, graphregion(color(white)) ycommon cols(3)
graph export "$out_figures/Figure_5.png", replace

